<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 Java synchronized 关键字</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .code-block {
            background: #2d3748;
            color: #e2e8f0;
            border-radius: 8px;
            overflow-x: auto;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        
        .concept-card {
            background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
            color: white;
            padding: 30px;
            border-radius: 16px;
            text-align: center;
            transition: transform 0.3s ease;
        }
        
        .concept-card:hover {
            transform: scale(1.05);
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 30px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <script>
        mermaid.initialize({ startOnLoad: true, theme: 'default' });
    </script>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-pulse">
                <i class="fas fa-lock mr-4"></i>synchronized 关键字
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                Java 多线程编程的基石，保障并发安全的守护者
            </p>
            <div class="flex justify-center space-x-8 text-lg">
                <div class="flex items-center">
                    <i class="fas fa-shield-alt mr-2"></i>
                    <span>线程安全</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-sync-alt mr-2"></i>
                    <span>同步机制</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-lock-open mr-2"></i>
                    <span>锁优化</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">当</span>涉及到多线程编程时，保证数据的正确性和一致性是至关重要的。而 <strong class="text-purple-600">synchronized</strong> 关键字是Java语言中最基本的同步机制之一，它可以有效地确保在多线程环境下共享资源的安全访问。<strong class="text-purple-600">synchronized</strong> 关键字可以应用于方法、代码块或静态方法上，用于实现对共享资源的同步访问。
            </p>
        </div>

        <!-- Concept Visualization -->
        <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-12">
            <div class="concept-card">
                <i class="fas fa-user-lock text-5xl mb-4"></i>
                <h3 class="text-2xl font-bold mb-2">对象锁</h3>
                <p>每个对象都有唯一的锁</p>
            </div>
            <div class="concept-card">
                <i class="fas fa-users text-5xl mb-4"></i>
                <h3 class="text-2xl font-bold mb-2">互斥访问</h3>
                <p>同一时刻只有一个线程访问</p>
            </div>
            <div class="concept-card">
                <i class="fas fa-redo text-5xl mb-4"></i>
                <h3 class="text-2xl font-bold mb-2">可重入性</h3>
                <p>同一线程可多次获取锁</p>
            </div>
        </div>

        <!-- Basic Concepts -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-book-open mr-3 text-purple-600"></i>基本概念
            </h2>
            
            <div class="space-y-6">
                <div class="bg-purple-50 rounded-lg p-6">
                    <h3 class="text-xl font-semibold mb-3 text-purple-800">
                        <i class="fas fa-shield-alt mr-2"></i>线程安全性
                    </h3>
                    <p class="text-gray-700 leading-relaxed">
                        在多线程编程中，当多个线程同时访问共享资源时，可能会出现数据竞争和不一致性的问题。<code class="bg-purple-200 px-2 py-1 rounded">synchronized</code> 关键字可以帮助解决这些问题，通过在关键代码块或方法前加上 <code class="bg-purple-200 px-2 py-1 rounded">synchronized</code> 关键字，可以确保同一时刻只有一个线程可以执行这段代码，从而避免了竞态条件（Race Condition）的发生。
                    </p>
                </div>

                <div class="bg-blue-50 rounded-lg p-6">
                    <h3 class="text-xl font-semibold mb-3 text-blue-800">
                        <i class="fas fa-lock mr-2"></i>对象锁
                    </h3>
                    <p class="text-gray-700 leading-relaxed">
                        <code class="bg-blue-200 px-2 py-1 rounded">synchronized</code> 关键字是基于对象的监视器锁（Monitor Lock）实现的。每个Java对象都可以作为一个锁，当一个线程进入 <code class="bg-blue-200 px-2 py-1 rounded">synchronized</code> 代码块或方法时，它会尝试获取对象的锁。如果锁已经被其他线程持有，则当前线程会被阻塞，直到获取到锁为止。
                    </p>
                </div>
            </div>
        </div>

        <!-- Usage Methods -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-code mr-3 text-purple-600"></i>使用方法
            </h2>

            <div class="space-y-8">
                <!-- Object Method Sync -->
                <div>
                    <h3 class="text-xl font-semibold mb-4 text-gray-800">
                        <i class="fas fa-cube mr-2 text-blue-600"></i>对象方法的同步
                    </h3>
                    <pre class="code-block p-4 overflow-x-auto"><code>public class SynchronizedExample {
    private int count = 0;

    // 同步方法
    public synchronized void increment() {
        count++;
    }

    // 非同步方法
    public void decrement() {
        count--;
    }

    public int getCount() {
        return count;
    }
}</code></pre>
                </div>

                <!-- Static Method Sync -->
                <div>
                    <h3 class="text-xl font-semibold mb-4 text-gray-800">
                        <i class="fas fa-layer-group mr-2 text-green-600"></i>静态方法的同步
                    </h3>
                    <pre class="code-block p-4 overflow-x-auto"><code>public class SynchronizedExample {
    private static int count = 0;

    // 静态同步方法
    public static synchronized void increment() {
        count++;
    }

    // 非静态方法
    public void decrement() {
        count--;
    }

    public static int getCount() {
        return count;
    }
}</code></pre>
                </div>

                <!-- Code Block Sync -->
                <div>
                    <h3 class="text-xl font-semibold mb-4 text-gray-800">
                        <i class="fas fa-th mr-2 text-orange-600"></i>代码块的同步
                    </h3>
                    <pre class="code-block p-4 overflow-x-auto"><code>public class SynchronizedExample {
    private Object lock = new Object();
    private int count = 0;

    public void increment() {
        synchronized (lock) {
            count++;
        }
    }

    public int getCount() {
        synchronized (lock) {
            return count;
        }
    }
}</code></pre>
                </div>
            </div>
        </div>

        <!-- Implementation Principle -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-cogs mr-3 text-purple-600"></i>实现原理
            </h2>
            
            <div class="mermaid">
                <div class="mermaid">
graph TD
    A[线程请求获取锁] --> B{锁是否可用?}
    B -->|是| C[获取锁]
    B -->|否| D[进入阻塞队列]
    C --> E[执行同步代码]
    E --> F[释放锁]
    F --> G[唤醒等待线程]
    D --> H[等待锁释放]
    H --> B
    
    style A fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
    style C fill:#48bb78,stroke:#333,stroke-width:2px,color:#fff
    style D fill:#f56565,stroke:#333,stroke-width:2px,color:#fff
    style F fill:#4299e1,stroke:#333,stroke-width:2px,color:#fff
                </div>
            </div>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-8">
                <div class="bg-gradient-to-r from-purple-100 to-pink-100 rounded-lg p-6">
                    <h4 class="font-semibold text-lg mb-3">
                        <i class="fas fa-microchip mr-2"></i>对象头标志位
                    </h4>
                    <p class="text-gray-700">Java对象的存储结构中包含了对象头信息，其中包括用于存储锁状态的标志位。</p>
                </div>
                <div class="bg-gradient-to-r from-blue-100 to-cyan-100 rounded-lg p-6">
                    <h4 class="font-semibold text-lg mb-3">
                        <i class="fas fa-memory mr-2"></i>内存可见性
                    </h4>
                    <p class="text-gray-700">当一个线程释放锁时，它所做的修改对其他线程都是可见的，确保内存一致性。</p>
                </div>
            </div>
        </div>

        <!-- Lock Granularity -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-ruler mr-3 text-purple-600"></i>锁的粒度
            </h2>

            <div class="highlight-box">
                <p class="text-lg">
                    <i class="fas fa-exclamation-circle mr-2"></i>
                    锁的粒度过细可能导致线程竞争过高，性能下